/* ************************************************************************

   qooxdoo - the new era of web development 
   qooxdoo - Web开发新纪元

   http://qooxdoo.org

   Copyright:
     2004-2008 1&1 Internet AG, Germany, http://www.1und1.de

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************

#resource(qx.static:qx/static)
#use(qx.lang.Core)
#use(qx.lang.Generics)

************************************************************************ */

/**
 * Create namespace
 * 创建命名空间
 */
qx =
{
  /**
   * Bootstrap qx.Bootstrap to create myself later
   * This is needed for the API browser etc. to let them detect me
   * 引导程序qx.Bootstrap在稍后创建自己
   * API浏览时必须引用
   */
  Bootstrap :
  {
    genericToString : function() {
      return "[Class " + this.classname + "]";
    },

    createNamespace : function(name, object)
    {
      var splits = name.split(".");
      var parent = window;
      var part = splits[0];

      for (var i=0, len=splits.length-1; i<len; i++, part=splits[i])
      {
        if (!parent[part]) {
          parent = parent[part] = {};
        } else {
          parent = parent[part];
        }
      }

      // store object 存储对象
      parent[part] = object;

      // return last part name (e.g. classname) 返回最后一个part名称(如类名)
      return part;
    },

	//define a Clazz with constructor or new Function
	//copy members to prototye of Clazz
	//copy statics to Clazz
	//assigns class full name to Clazz.classname
	//assigns class short name to Clazz.basename
	//assigns 'Class' to Clazz.$$type
	//Execute defer section
	//attachs toString method for Clazz
    define : function(name, config)
    {
      if (!config) {
        var config = { statics : {} };
      }

      var clazz;
      var proto = null;

      if (config.members)
      {
        clazz = config.construct || new Function;
        var statics = config.statics;
        for (var key in statics) {
          clazz[key] = statics[key];
        }

        proto = clazz.prototype;
        var members = config.members;
        for (var key in members) {
          proto[key] = members[key];
        }
      }
      else
      {
        clazz = config.statics || {};
      }

      // Create namespace
      var basename = this.createNamespace(name, clazz);

      // Store names in constructor/object
      clazz.name = clazz.classname = name;
      clazz.basename = basename;

      // Store type info
      clazz.$$type = "Class";

      // Attach toString 如果类无toString方法则给其加上
      if (!clazz.hasOwnProperty("toString")) {
        clazz.toString = this.genericToString;
      }

      // Execute defer section 执行延迟块
      if (config.defer) {
        config.defer(clazz, proto);
      }

      // Store class reference in global class registry
      // 存储类引用到全局类登记册
      qx.Bootstrap.$$registry[name] = config.statics;
    }
  }
};


/**
 * Internal class that is responsible for bootstrapping the qooxdoo
 * framework at load time.
 * 在载入时引导qooxdoo框架
 *
 * Automatically loads JavaScript language fixes and enhancements to
 * bring all engines to at least JavaScript 1.6.
 * 自动载入JavaScript修复和增强，使所有引擎至少支持到JavaScript1.6
 *
 * Does support:
 * 支持：
 *
 * * Statics 静态成员
 * * Members 成员
 * * Defer 延迟
 *
 * Does not support:
 * 不支持：
 *
 * * Custom extends 继承
 * * Super class calls 调用父类
 * * Mixins, Interfaces, Properties, ...|中文| 混入，接口，属性，...
 */
qx.Bootstrap.define("qx.Bootstrap",
{
  statics :
  {
    /** Timestamp of qooxdoo based application startup|中文|qooxdoo应用程序开始时间 */
    LOADSTART : new Date,


    /**
     * Creates a namespace and assigns the given object to it.
     * 创建一个命名空间并赋予一个给定的对象
     * Lightweight version of {@link qx.Class#createNamespace} only used during bootstrap phase.
     * qx.Class#createNamespace的轻量级版本，仅在引导阶段使用
     *
     * @internal
     * @param name {String} The complete namespace to create. Typically, the last part is the class name itself
     * @param object {Object} The object to attach to the namespace
     * @return {Object} last part of the namespace (typically the class name)
     * @throws an exception when the given object already exists.
     */
    createNamespace : qx.Bootstrap.createNamespace,


    /**
     * Define a new class using the qooxdoo class system.
     * 使用qooxdoo的类定义一个新类
     * Lightweight version of {@link qx.Class#define} only used during bootstrap phase.
     * qx.Class#define的轻量级版本，仅在引导阶段使用
     *
     * @internal
     * @signature function(name, config)
     * @param name {String} Name of the class
     * @param config {Map ? null} Class definition structure.
     * @return {void}
     */
    define : qx.Bootstrap.define,


    /**
     * This method will be attached to all classes to return
     * a nice identifier for them.
     * 返回类标识符，此方法会被附加到所有类
     *
     * @internal
     * @signature function()
     * @return {String} The class identifier
     */
    genericToString : qx.Bootstrap.genericToString,


    /**
     * Find a class by its name ｜中文｜获取指定名称的类
     *
     * @param name {String} class name to resolve
     * @return {Class} the class
     */
    getByName : function(name) {
      return this.$$registry[name];
    },


    /** {Map} Stores all defined classes */
    $$registry : {}
  }
});
